package j_util.database.gui;

import j_util.database.testing.SpecificDBCreator;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
public class MainFrame extends JFrame {
    private javax.swing.JButton connectButton;
    private javax.swing.JLabel connectedLabel;
    private javax.swing.JButton createTableButton;
    private javax.swing.JButton deletePredefinedDataButton;
    private javax.swing.JButton dialogButton;
    private javax.swing.JButton dropTableButton;
    private javax.swing.JButton enterPredefinedDataButton;
    private javax.swing.JButton executeQuery;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JButton modifyPredefinedDataButton;
    private javax.swing.JButton openSQLConsoleButton;
    private javax.swing.JButton showInformationButton;
    private boolean connected;

    public MainFrame() {
        initComponents();
    }

    private void initComponents() {

        connectButton = new javax.swing.JButton();
        createTableButton = new javax.swing.JButton();
        dropTableButton = new javax.swing.JButton();
        enterPredefinedDataButton = new javax.swing.JButton();
        modifyPredefinedDataButton = new javax.swing.JButton();
        deletePredefinedDataButton = new javax.swing.JButton();
        openSQLConsoleButton = new javax.swing.JButton();
        showInformationButton = new javax.swing.JButton();
        executeQuery = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        dialogButton = new javax.swing.JButton();
        connectedLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        connectButton.setText("Connect to DB");
        connectButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (connected) {
                    connectButton.setText("Connect to DB");
                    connected = false;
                    SpecificDBCreator.disconnect();
                    connectedLabel.setText("Disconnected");
                } else {
                    SpecificDBCreator.createPredefinedConnection();
                    connectButton.setText("Disconnect from DB");
                    connected = true;
                    connectedLabel.setText("Connected");
                }
            }
        });

        createTableButton.setText("Create table");
        createTableButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    SpecificDBCreator.createPersonaTable();
                    jTextArea1.setText("Table PERSONA created succesfuly!");
                } catch (SQLException e1) {
                    jTextArea1.setText("An error ocurred!\n");
                }
            }
        });

        dropTableButton.setText("Drop table");
        dropTableButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    SpecificDBCreator.dropPersonaTable();
                    jTextArea1.setText("Table PERSONA dropped succesfuly!");
                } catch (SQLException e1) {
                    jTextArea1.setText("An error ocurred!\n");
                }
            }
        });

        enterPredefinedDataButton.setText("Enter predefined data");
        enterPredefinedDataButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    SpecificDBCreator.insertPredefinedData();
                    jTextArea1.setText("Data entered succesfully!");
                } catch (SQLException e1) {
                    jTextArea1.setText("An error ocurred!\n");
                }
            }
        });

        modifyPredefinedDataButton.setText("Modify predefined data");
        modifyPredefinedDataButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    SpecificDBCreator.modifyPredefinedData();
                    jTextArea1.setText("Data modified succesfully!");
                } catch (SQLException e1) {
                    jTextArea1.setText("An error ocurred!\n");
                }
            }
        });

        deletePredefinedDataButton.setText("Delete predefined data");
        deletePredefinedDataButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    SpecificDBCreator.deletePredefinedData();
                    jTextArea1.setText("Data deleted succesfully!");
                } catch (SQLException e1) {
                    jTextArea1.setText("An error ocurred!\n");
                }
            }
        });

        openSQLConsoleButton.setText("Open SQL console");

        showInformationButton.setText("Show DB information");
        showInformationButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
                    StringBuffer text = new StringBuffer();
                    text.append("Table name: PERSONA\nTable columns: \n");
                    List<String> columns = SpecificDBCreator.getColumnNames("PERSONA");
                    for (int j = 0; j < columns.size(); j++) {
                        text.append(columns.get(j) + "\n");
                    }
                    text.append("Table name: PERSONA\nData: \n");
                    List<String> list = SpecificDBCreator.getPredefinedInformation();
                    for (int j = 0; j < list.size(); j++) {
                        text.append(list.get(j) + "\n");
                    }
                    jTextArea1.setText(text.toString());
                } catch (SQLException e) {
                    jTextArea1.setText("An error ocurred!\n");
                }
            }
        });

        executeQuery.setText("Execute predefined query");
        executeQuery.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    StringBuffer text = new StringBuffer();
                    text.append("Table name: PERSONA\nTable columns: \n");
                    List<String> columns = SpecificDBCreator.getColumnNames("PERSONA");
                    for (int j = 0; j < columns.size(); j++) {
                        text.append(columns.get(j) + "\n");
                    }
                    text.append("Table name: PERSONA\nData: \n");
                    List<String> list = SpecificDBCreator.executePredefinedQuery2();
                    for (int j = 0; j < list.size(); j++) {
                        text.append(list.get(j) + "\n");
                    }
                    jTextArea1.setText(text.toString());
                } catch (SQLException e1) {
                    jTextArea1.setText("An error ocurred!\n");
                }
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        dialogButton.setText("Custom dialog");
        dialogButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                PersonDialog personDialog = new PersonDialog();
                personDialog.setModal(true);
                personDialog.setVisible(true);
            }
        });

        connectedLabel.setText("Disconnected");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(enterPredefinedDataButton, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                                                .addComponent(dropTableButton, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                                                .addComponent(createTableButton, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                                                .addComponent(connectButton, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                                                .addComponent(modifyPredefinedDataButton, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                .addComponent(dialogButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(executeQuery, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(showInformationButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(openSQLConsoleButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(deletePredefinedDataButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 617, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(connectedLabel))
                        .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup()
                                .addComponent(connectButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(createTableButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(dropTableButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(enterPredefinedDataButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(modifyPredefinedDataButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                                .addComponent(executeQuery)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(deletePredefinedDataButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(openSQLConsoleButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(showInformationButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(dialogButton)
                                .addGap(11, 11, 11)))
                        .addGap(1, 1, 1)
                        .addComponent(connectedLabel))
        );

        pack();
    }

    public static void main(String[] args) {
        MainFrame mainFrame = new MainFrame();
        mainFrame.setVisible(true);
    }
}
